#!/bin/bash
xhost +

# 设备检测
DEPTH_PATH=$(readlink -f /dev/astradepth 2>/dev/null || echo "")
UVC_PATH=$(readlink -f /dev/astrauvc 2>/dev/null || echo "")

# 构建Docker参数
DOCKER_ARGS=(
    "docker" "run" "-it"
    "--net=host"
    "--env=DISPLAY"
    "--env=QT_X11_NO_MITSHM=1"
    "-v" "$HOME/fastrtps_unicast.xml:/root/fastrtps_unicast.xml"
    "-v" "/tmp/.X11-unix:/tmp/.X11-unix"
    "-v" "$HOME/yahboomcar_ros2_ws/temp/:/root/yahboomcar_ros2_ws/temp/"
    "-v" "$HOME/muto_xzt/:/root/muto_xzt/"
    "-v" "$HOME/muto-llm/:/root/muto-llm/"
    "--device=/dev/myserial"
    "--device=/dev/myspeech"
    "--device=/dev/snd"
    "--device=/dev/rplidar"
    "--device=/dev/input"
    "-e" "OMP_NUM_THREADS=$(nproc)"
    "-e" "TORCH_NUM_THREADS=$(nproc)"
    "-p" "9090:9090"
    "-p" "8888:8888"
    "-e" "RMW_IMPLEMENTATION=rmw_cyclonedx_cpp"
)

# 添加设备路径
if [ -n "$DEPTH_PATH" ]; then
    DOCKER_ARGS+=("--device=$DEPTH_PATH")
fi

if [ -n "$UVC_PATH" ]; then
    DOCKER_ARGS+=("--device=$UVC_PATH")
fi

# 添加镜像和命令
DOCKER_ARGS+=("yahboomtechnology/ros-foxy-muto:2.9.18" "/bin/bash" "-c")


# 构建初始化脚本
INIT_SCRIPT=""
if [ -n "$DEPTH_PATH" ]; then
    INIT_SCRIPT="$INIT_SCRIPT ln -sf $DEPTH_PATH /dev/astradepth;"
fi
if [ -n "$UVC_PATH" ]; then
    INIT_SCRIPT="$INIT_SCRIPT ln -sf $UVC_PATH /dev/astrauvc;"
fi
INIT_SCRIPT="$INIT_SCRIPT cd ~; /bin/bash"

DOCKER_ARGS+=("$INIT_SCRIPT")

# 执行Docker命令
"${DOCKER_ARGS[@]}"
